import http from '@/api/http'

//等份拆分
/**
 * @param {Object[]} arr - 原数组
 * @param {number} size - 几个一组
 * @description 将数组等分拆组
 * @returns {array}
 */
function chunkArray(arr, size) {
  let result = []
  let firstArr = []
  if (size > 18) {
    firstArr = arr.splice(0, 18)
    for (let i = 0; i < arr.length; i += size) {
      result.push(arr.slice(i, i + size))
    }
  } else {
    firstArr = arr
  }

  result.unshift(firstArr)
  return result
}

//判断材料是否过期
/**
 * @param {string} type - 类型
 * @param {Object} row - 当前行数据
 * @description 判断材料是否过期
 * @returns {Object}
 */
function isOutDate(type, row) {
  let AddTime = new Date(row.AddTime || row.CreateDate).getTime()
  let currentTime = new Date().getTime()
  if (type == '产成品' || type == '原材料' || type == '包辅材') {
    let expirationTime = 1000 * 60 * 60 * 24 * 30 * 3 //3个月毫秒数
    let CriticalTime = expirationTime - 1000 * 60 * 60 * 24 * 10 //过期时间往前退十天是临期时间
    let timeRemianing = Math.floor(
      (AddTime + expirationTime - currentTime) / 1000 / 60 / 60 / 24
    ) //剩余天数
    if (currentTime >= AddTime + expirationTime)
      return {
        tip: '已过期',
        timeRemianing,
      }
    if (currentTime >= AddTime + CriticalTime)
      return {
        tip: '临期',
        timeRemianing,
      }
    return {
      tip: '未过期',
      timeRemianing,
    }
  }

  if (type == '半成品') {
    let expirationTime = 1000 * 60 * 60 * 24 * 30 //30天毫秒数
    let CriticalTime = expirationTime - 1000 * 60 * 60 * 24 * 10 //过期时间往前退十天是临期时间
    let timeRemianing = Math.floor(
      (AddTime + expirationTime - currentTime) / 1000 / 60 / 60 / 24
    ) //剩余天数
    if (currentTime >= AddTime + expirationTime)
      return {
        tip: '已过期',
        timeRemianing,
      }
    if (currentTime >= AddTime + CriticalTime)
      return {
        tip: '临期',
        timeRemianing,
      }
    return {
      tip: '未过期',
      timeRemianing,
    }
  }
}

//获取数据字典
/**
 * @param {string[]} dataKeyList - 字典值数组
 * @description 获取数据字典
 * @returns {Object}
 */
async function getDatakey(dataKeyList) {
  let res = await http.post(
    'api/Sys_Dictionary/GetVueDictionary',
    dataKeyList,
    true
  )
  return res
}

//解锁物料
/**
 * @param {string[]} row - 当前行数据
 * @description 解锁物料
 * @returns {Object}
 */
async function unlockMaterial(id, component, stype) {
  const res = await http.post(
    `/api/Ware_RawMaterial/UpdateStateUnlock?id=${id}&type=${stype}`
  )
  if (res.status) {
    component.$message.success('解锁成功')
    component.search()
  } else {
    component.$message.error(res.message)
  }
}

//锁定物料
/**
 * @param {string[]} row - 当前行数据
 * @description 锁定物料
 * @returns {Object}
 */
async function lockMaterial(id, component, stype) {
  const res = await http.post(
    `/api/Ware_RawMaterial/UpdateStateLock?id=${id}&type=${stype}`
  )
  if (res.status) {
    component.$message.success('锁定成功')
    component.search()
  } else {
    component.$message.error(res.message)
  }
}

export { chunkArray, isOutDate, getDatakey, unlockMaterial, lockMaterial }
